package formbeans;

import java.util.ArrayList;
import java.util.List;

import org.mybeans.form.FormBean;

public class RegisterForm extends FormBean {

	private String userid;
	private String password;
	private String confirmpassword;
	private String type;
	
	public String getUserid() {
		return userid;
	}
	public String getPassword(){ 
		return password; 
    }
	public String getConfirmpassword() {
		return confirmpassword;
	}
	public String getType() {
		return type;
	}

	public void setUserid(String userid) {
		this.userid = trimAndConvert(userid,"<>\"");
	}
	public void setPassword(String password) {	
		this.password = trimAndConvert(password,"<>\"");
	}
	public void setConfirmpassword(String confirmpassword) {
		this.confirmpassword = trimAndConvert(confirmpassword,"<>\"");
	}
	public void setType(String type) {
		this.type = type;
	}
	
	public List<String> checkRegisterFormErrors(){
		List<String> errors = new ArrayList<String>();
		
		if(userid == null || userid.length() == 0){
			errors.add("ID is required");
		} else if (userid.length() >= 255) {
			errors.add("ID is too long.");
		}
		if(password == null || password.length() == 0){
			errors.add("Password is required");
		} else if (password.length() >= 20) {
			errors.add("Password's length is 1-20, please re-enter.");
		}
		if (!password.equals(confirmpassword)) {
			errors.add("Passwords do not match");
		}
		if (isEmpty(type)) {
			errors.add("Choose user type");
			return errors;
		}
		if (!(type.equals("Researcher") || type.equals("Respondent"))) {
			errors.add("Choose either Researcher or Respondent");
		}
	
		return errors;
	}
	
	private boolean isEmpty(String str) {
		return (str == null || str.length() == 0);
	}
}